package com.vladimirkolarevic.eventplanner.spring_hibernate;

import java.io.Serializable;
import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;

import com.vladimirkolarevic.eventplanner.Comment;
import com.vladimirkolarevic.eventplanner.Event;
import com.vladimirkolarevic.eventplanner.User;
import com.vladimirkolarevic.eventplanner.beans.EventBean;
import com.vladimirkolarevic.eventplanner.service.EventsService;

public class EventDAO implements EventsService {

	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	@Transactional
	public Long addEvent(Event event) {
		// TODO Auto-generated method stub
		return (Long) sessionFactory.getCurrentSession().save(event);
	}

	public void atach(Event event) {
		// TODO Auto-generated method stub

	}
	@Transactional
	public void delete(Comment comment) {
		// TODO Auto-generated method stub
		sessionFactory.getCurrentSession().delete(comment);
	}
	@Transactional
	public void delete(User user) {
		// TODO Auto-generated method stub
		sessionFactory.getCurrentSession().delete(user);
	}
	@Transactional
	public List<Event> retrive() {
		// TODO Auto-generated method stub
		return sessionFactory.getCurrentSession().createCriteria(EventBean.class).list();
		 
	}
	@Transactional
	public Event retriveEvent(Serializable id) {
		// TODO Auto-generated method stub
		return (Event) sessionFactory.getCurrentSession().load(EventBean.class, id);
	}
	@Transactional
	public void update(Event event) {
		// TODO Auto-generated method stub
		Event e = (Event) sessionFactory.getCurrentSession().merge(event);
		sessionFactory.getCurrentSession().update(e);
	}

}
